//
//  DetailCell.m
//  CMJWeibo
//
//  Created by BH on 16/3/11.
//
//

#import "DetailCell.h"
#import "CMJCommon.h"
#import "UIImage+MJ.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "UIView+MJ.h"

@interface DetailCell ()

//头像
@property(nonatomic,strong)UIImageView *iconV;
//会员
@property(nonatomic,strong)UIImageView *vipV;
//昵称
@property(nonatomic,strong)UILabel *nameL;
//发布时间
@property(nonatomic,strong)UILabel *createTimeL;
//内容
@property(nonatomic,strong)UILabel *contentL;

@end

@implementation DetailCell

+(instancetype)cellWithTableView:(UITableView *)tableview
{
    static NSString *identifier = @"detailCell";
    DetailCell *cell = [tableview dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[DetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.separatorInset = UIEdgeInsetsMake(0, 0, 0, 10);
    }
    return cell;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _iconV = [[UIImageView alloc] init];
        [self.contentView addSubview:_iconV];
        
        _vipV = [[UIImageView alloc] init];
        _vipV.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_vipV];
        
        _nameL = [[UILabel alloc] init];
        _nameL.font = NicknameFont;
        [self.contentView addSubview:_nameL];
        
        _createTimeL = [[UILabel alloc] init];
        _createTimeL.font = TimeFont;
        _createTimeL.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_createTimeL];
        
        _contentL = [[UILabel alloc] init];
        _contentL.font = ContentFont;
        _contentL.numberOfLines = 0;
        _contentL.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_contentL];
    };
    
    return self;
}

-(void)setDetailM:(DetailModel *)detailM
{
    _detailM = detailM;
    
    //设置显示data
    [self setData];
    
    //设置frame
    [self setFrame];
    
    //cell高度
    _cellHeight = CGRectGetMaxY(_contentL.frame)+10;
}

-(CGFloat)height:(DetailModel *)model
{
    [self setDetailM:model];
    
    return self.cellHeight;
}

/** 设置数据 */
-(void)setData
{
    //头像
    NSURL *iconUrl = [NSURL URLWithString:_detailM.user.profile_image_url];
    [_iconV sd_setImageWithURL:iconUrl placeholderImage:[UIImage imageWithName:@"avatar_default_small"]];
    
    //是否会员
    if ([_detailM.user isVip]) {
        //会员
        NSString *imageName = [NSString stringWithFormat:@"common_icon_membership_level%ld",_detailM.user.mbrank];
        self.vipV.image = [UIImage imageWithName:imageName];
        self.nameL.textColor = [UIColor orangeColor];
        self.vipV.hidden = NO;
    }else{
        //不是会员
        self.vipV.hidden = YES;
        self.nameL.textColor = [UIColor blackColor];
    }
    //评论者昵称
    self.nameL.text = _detailM.user.name;
    //评论发布时间
    self.createTimeL.text = _detailM.created_at;
    //评论内容
    self.contentL.text = _detailM.text;
}

/** 设置frame */
-(void)setFrame
{
    //头像
    _iconV.frame = CGRectMake(CellPadding, CellPadding, 35, 35);
    _iconV.layer.cornerRadius = _iconV.width*0.5;
    
    //昵称
    _nameL.x = CGRectGetMaxX(_iconV.frame)+CellPadding;
    _nameL.y = _iconV.y;
    _nameL.size = [_detailM.user.name sizeWithFont:NicknameFont];
    
    //会员图标
    _vipV.x = CGRectGetMaxX(_nameL.frame)+CellPadding;
    _vipV.y = _nameL.y;
    _vipV.size = CGSizeMake(14, _nameL.height);
    
    //时间
    _createTimeL.x = _nameL.x;
    _createTimeL.y = CGRectGetMaxY(_nameL.frame)+CellPadding*0.5;
    _createTimeL.size = [_detailM.created_at sizeWithFont:TimeFont];
    
    //正文
    _contentL.x = _createTimeL.x;
    _contentL.y = CGRectGetMaxY(_createTimeL.frame)+CellPadding;
    CGFloat contentMaxW = ScreenWidth-CellPadding*3-_iconV.width;
    CGSize contentMaxSize = CGSizeMake(contentMaxW, MAXFLOAT);
    _contentL.size = [_detailM.text sizeWithFont:ContentFont constrainedToSize:contentMaxSize];
}

@end
